package dao

import (
	__ "base_srv/basic/baseproto"
	"base_srv/handler/models"
	"errors"
	"time"
)

func PassengerAdd(in *__.PassengerAppAddReq) (uint32, error) {
	t, err := time.Parse(time.DateTime, in.StartUpdateTime)
	if err != nil {
		return 0, errors.New("时间格式错误")
	}
	a := models.PassengerAppUpdate{
		PassengerAppName:    in.PassengerAppName,
		PassengerAppVersion: in.PassengerAppVersion,
		LaunchSystem:        int8(in.LaunchSystem),
		IsForceUpdate:       int8(in.IsForceUpdate),
		UpdateText:          in.UpdateText,
		UserId:              int32(in.UserId),
		UploadTime:          time.Now(),
		StartUpdateTime:     t.Add(-time.Hour * 8),
	}
	if err = a.Create(); err != nil {
		return 0, errors.New("添加失败:" + err.Error())

	}
	return a.Id, nil
}

// 乘客端app更新
func PassengerUpdate(in *__.PassengerAppUpdateReq) error {
	var t time.Time
	if in.StartUpdateTime != "" {
		t, _ = time.Parse(time.DateTime, in.StartUpdateTime)
	}
	a := models.PassengerAppUpdate{
		Id:                  uint32(in.Id),
		PassengerAppName:    in.PassengerAppName,
		PassengerAppVersion: in.PassengerAppVersion,
		LaunchSystem:        int8(in.LaunchSystem),
		IsForceUpdate:       int8(in.IsForceUpdate),
		UpdateText:          in.UpdateText,
		UserId:              int32(in.UserId),
		UploadTime:          time.Now(),
		StartUpdateTime:     t.Add(-time.Hour * 8),
	}
	if err := a.Update(); err != nil {
		return errors.New("更新失败:" + err.Error())
	}
	return nil
}

// 乘客端app应用列表
func PassengerAppList() ([]*__.PassengerAppList, error) {
	var a models.PassengerAppUpdate
	list, err := a.UpdateList()
	if err != nil {
		return nil, errors.New("查询失败:" + err.Error())
	}
	var respList []*__.PassengerAppList
	for _, v := range list {
		l := __.PassengerAppList{
			Id:                  int64(v.Id),
			PassengerAppName:    v.PassengerAppName,
			PassengerAppVersion: v.PassengerAppVersion,
			LaunchSystem:        int32(v.LaunchSystem),
			IsForceUpdate:       int32(v.IsForceUpdate),
			UpdateText:          v.UpdateText,
			UserName:            v.RealName,
			UploadTime:          v.UploadTime.Format("2006-01-02 15:04:05"),
			StartUpdateTime:     v.StartUpdateTime.Format("2006-01-02 15:04:05"),
		}
		respList = append(respList, &l)
	}
	return respList, nil
}

// 乘客端app删除
func PassengerAppDel(id int) error {
	var p models.PassengerAppUpdate
	p.Id = uint32(id)
	if err := p.Delete(); err != nil {
		return errors.New("删除失败:" + err.Error())
	}
	return nil
}

// 司机端app添加
func DriverAppAdd(in *__.DriverAppAddReq) (uint32, error) {
	t, err := time.Parse(time.DateTime, in.StartUpdateTime)
	if err != nil {
		return 0, errors.New("时间格式错误")
	}
	a := models.DriverAppUpdate{
		Id:               0,
		DriverAppName:    in.DriverAppName,
		DriverAppVersion: in.DriverAppVersion,
		LaunchSystem:     int8(in.LaunchSystem),
		IsForceUpdate:    int8(in.IsForceUpdate),
		UpdateText:       in.UpdateText,
		UserId:           int32(in.UserId),
		UploadTime:       time.Now(),
		StartUpdateTime:  t.Add(-time.Hour * 8),
	}
	if err = a.Create(); err != nil {
		return 0, errors.New("添加失败:" + err.Error())
	}
	return a.Id, nil
}

// 司机端app列表

func DriverAppList() ([]*__.DriverAppList, error) {
	var a models.DriverAppUpdate
	list, err := a.UpdateList()
	if err != nil {
		return nil, errors.New("查询失败:" + err.Error())
	}
	var respList []*__.DriverAppList
	for _, v := range list {
		l := __.DriverAppList{
			Id:               int64(v.Id),
			DriverAppName:    v.DriverAppName,
			DriverAppVersion: v.DriverAppVersion,
			LaunchSystem:     int32(v.LaunchSystem),
			IsForceUpdate:    int32(v.IsForceUpdate),
			UpdateText:       v.UpdateText,
			UserName:         v.RealName,
			UploadTime:       v.UploadTime.Format("2006-01-02 15:04:05"),
			StartUpdateTime:  v.StartUpdateTime.Format("2006-01-02 15:04:05"),
		}
		respList = append(respList, &l)
	}
	return respList, nil
}

// 司机端app更新
func DriverAppUpdate(in *__.DriverAppUpdateReq) error {
	var t time.Time
	if in.StartUpdateTime != "" {
		t, _ = time.Parse(time.DateTime, in.StartUpdateTime)
	}
	a := models.DriverAppUpdate{
		Id:               uint32(in.Id),
		DriverAppName:    in.DriverAppName,
		DriverAppVersion: in.DriverAppVersion,
		LaunchSystem:     int8(in.LaunchSystem),
		IsForceUpdate:    int8(in.IsForceUpdate),
		UpdateText:       in.UpdateText,
		UserId:           int32(in.UserId),
		UploadTime:       time.Now(),
		StartUpdateTime:  t.Add(-time.Hour * 8),
	}
	if err := a.Update(); err != nil {
		return errors.New("更新失败:" + err.Error())
	}
	return nil
}

// 司机端app删除
func DriverAppDel(id int) error {
	var p models.DriverAppUpdate
	p.Id = uint32(id)
	if err := p.Delete(); err != nil {
		return errors.New("删除失败:" + err.Error())
	}
	return nil
}

// 车机端app添加
func CarAppAdd(in *__.CarAppUpdateAddReq) (uint32, error) {
	res, err := time.Parse(time.DateTime, in.StartUpdateTime)
	if err != nil {
		return 0, errors.New("时间格式错误")
	}
	c := models.CarAppUpdate{
		CarAppName:      in.CarAppName,
		CarAppVersion:   in.CarAppVersion,
		VersionCode:     in.VersionCode,
		UpdateText:      in.UpdateText,
		UserId:          int32(in.UserId),
		UploadTime:      time.Now(),
		StartUpdateTime: res.Add(-time.Hour * 8),
	}

	if err = c.Create(); err != nil {
		return 0, errors.New("添加失败:" + err.Error())
	}
	return c.Id, err
}

// 车机端app列表
func CarAppList() ([]*__.CarAppUpdateList, error) {
	var c models.CarAppUpdate
	list, err := c.GetList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var respList []*__.CarAppUpdateList
	for _, v := range list {
		l := __.CarAppUpdateList{
			Id:              int64(v.Id),
			CarAppName:      v.CarAppName,
			CarAppVersion:   v.CarAppVersion,
			VersionCode:     v.VersionCode,
			UpdateText:      v.UpdateText,
			UserName:        v.RealName,
			StartUpdateTime: v.StartUpdateTime.Format(time.DateTime),
			UploadTime:      v.UploadTime.Format(time.DateTime),
		}
		respList = append(respList, &l)
	}
	return respList, nil
}

// 车机端app更新
func CarAppUpdate(in *__.CarAppUpdateReq) error {
	var t time.Time
	if in.StartUpdateTime != "" {
		t, _ = time.Parse(time.DateTime, in.StartUpdateTime)
	}
	o := models.CarAppUpdate{
		Id:              uint32(in.Id),
		CarAppName:      in.CarAppName,
		CarAppVersion:   in.CarAppVersion,
		VersionCode:     in.VersionCode,
		UpdateText:      in.UpdateText,
		UserId:          int32(in.UserId),
		UploadTime:      time.Now(),
		StartUpdateTime: t.Add(-time.Hour * 8),
	}
	if err := o.Updates(); err != nil {
		return errors.New("更新失败:" + err.Error())
	}
	return nil
}

// 车机端app删除
func CarAppDel(id int) error {
	var c models.CarAppUpdate
	c.Id = uint32(id)
	if err := c.Delete(); err != nil {
		return errors.New("删除失败:" + err.Error())
	}
	return nil
}
